<template>
    <div id="seachDialog">
      <a-modal
        title="作废"
        v-model="invalidData.show"
        width="520px"
        style="overflow:hidden"
        @cancel="handleClose">
        <el-row style="padding:20px 30px">
            <el-form ref="Form" size="small" :model="parames" :rules="rules" label-width="100px">
                <el-form-item label="作废原因" prop="invalidReason">
                    <el-select size="small" v-model="parames.invalidReason" placeholder="请选择">
                        <el-option v-for="(item,index) in dataList" :key="index" :label="item.invalidReasonName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="作废原因说明">
                    <el-input v-model="parames.invalidDescription"></el-input>
                </el-form-item>
            </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="handleClose">取消</el-button>
            <el-button size="small" type="primary" @click="handleSave">保存</el-button>
        </span>
      </a-modal>
    </div>
</template>

<script>
export default {
    props: ['invalidData'],
    data() {
        return {
          parames: {
              invalidReason: '',//"作废原因",
              invalidDescription: '',//"作废原因说明"
          },
          dataList: [],
          rules: {
            invalidReason: [{ required: true, message: '必填', trigger: ['change','blur' ]}],
          }
        };
    },
    methods: {
        handleClose () {
            this.invalidData.show = false
        },
        handleSave () {
            this.$refs['Form'].validate((valid) => {
                if (valid) {
                    if (this.invalidData.type == 'piliang') {
                        this.$post(this.$api.postOpportunityBatchInvalid,{
                            opportunityIds: this.invalidData.id,
                            invalidReason: this.parames.invalidReason,
                            invalidDescription: this.parames.invalidDescription,
                        }).then(response => {
                            if (response.status == 200) {
                                this.handleClose()
                                this.$message({type:'success',message:'操作成功'})
                                setTimeout(() => {
                                    this.$emit('checkRender')
                                }, 200)
                            }
                        })
                    }
                    if (this.invalidData.type == 'one') {
                        this.$post(this.$api.postOpportunityInvalid,{
                            opportunityId: this.invalidData.id,
                            invalidReason: this.parames.invalidReason,
                            invalidDescription: this.parames.invalidDescription,
                        }).then(response => {
                            if (response.data.succeed) {
                                this.handleClose()
                                this.$message({type:'success',message:'操作成功'})
                                setTimeout(() => {
                                    this.$emit('checkOpen','2')
                                }, 200)
                            }
                        })
                    }
                    if (this.invalidData.type == 'hetong') {
                        this.$post(this.$api.postContactInvalid,{
                            contractId: this.invalidData.id,
                            invalidReason: this.parames.invalidReason,
                            invalidDescription: this.parames.invalidDescription,
                        }).then(response => {
                            if (response.data.succeed) {
                                this.handleClose()
                                this.$message({type:'success',message:'操作成功'})
                                setTimeout(() => {
                                    this.$emit('checkOpen','2')
                                }, 200)
                            }
                        })
                    }
                }       
            })
        },
        resetData () {
            this.parames.invalidReason = ''
            this.parames.invalidDescription = ''
            this.$refs.Form.resetFields()
        }
    },
    computed: {
      changeDialog () {
        return this.invalidData.show
      }
    }, 
    watch: {
      changeDialog: {
        handler (curVal, oldVal) {
          if (curVal) {
              this.$post(this.$api.postInvalidReasonList,this.parames).then(response => {
                if (response.data.succeed) {
                    this.dataList = response.data.data
                }
            })
          } else {
            this.resetData()
          }
        }
      }
    }
};
</script>

<style>
#seachDialog .el-dialog{
    padding-bottom: 0; 
    overflow-x: hidden;
}
#seachDialog .el-dialog__footer{
    border-top: #f4f4f4 solid 1px;
}
</style>
